using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Trigits
{
    public class SlateContainer
    {
        private String playerName = "";
        private int score = 0;
        private int round = 0;
        public TriominoeContainer container;
        private SpriteFont font;
        private Rectangle bounds;
        private Rectangle boundsContainer;
        private int max;

        private Texture2D boardBackground;
        private Texture2D boardActiveBackground;

        private PassButton passButton;

        public SlateContainer(SpriteFont font, int max, Rectangle bounds,
            Rectangle boundsContainer,
            Texture2D boardBackground, Texture2D boardActiveBackground,
            PassButton passButton)
        {
            this.font = font;
            this.bounds = bounds;
            this.boundsContainer = boundsContainer;
            this.max = max;
            this.boardBackground = boardBackground;
            this.boardActiveBackground = boardActiveBackground;

            this.passButton = passButton;
            passButton.setBounds(new Rectangle(bounds.Width - bounds.X, bounds.Y, 93, 31));

            container = new TriominoeSlate(boundsContainer, max, font);
        }


        public void flipAllTrio(bool flip)
        {
            Triominoe[] triminoes = container.getTriominoes();
                foreach (Triominoe t in triminoes)
                {
                    if(t != null)
                        t.flip(flip);
                }
        }
        public Boolean checkTrioInBoundsContainer(Vector2 v)
        {
            if (v.X > boundsContainer.X && v.X < boundsContainer.X + boundsContainer.Width &&
                v.Y > boundsContainer.Y && v.Y < boundsContainer.Y + boundsContainer.Height)
                return true;

            return false;
        }
        public void Update(GameTime gameTime, Triominoe inHand)
        {
            container.update(gameTime, inHand);
        }
        public void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch, SpriteFont font, BasicEffect basicEffect, bool active)
        {
            spriteBatch.Begin();
                basicEffect.CurrentTechnique.Passes[0].Apply();
                spriteBatch.Draw(active ? boardActiveBackground : boardBackground, bounds, Color.White);
            spriteBatch.End();

            spriteBatch.Begin();
                basicEffect.CurrentTechnique.Passes[0].Apply();
                spriteBatch.DrawString(font, playerName, new Vector2(bounds.X + 20f, bounds.Y+15f), Color.White);
                spriteBatch.DrawString(font, "Score: " + score, new Vector2(bounds.X  + 20f, bounds.Y + 35f), Color.White);
                //spriteBatch.DrawString(font, "Round: " + round, new Vector2(bounds.X  + 20f, bounds.Y + 50f), Color.White);
            spriteBatch.End();

            passButton.Draw(graphics, spriteBatch);

            container.draw(graphics, spriteBatch, font, basicEffect);
        }

        public bool hasTriominoe(Triominoe triominoe)
        {
            foreach (Triominoe t in container.getTriominoes())
            {
                if (t == triominoe)
                    return true;
            }

            return false;
        }
        public Boolean checkInContainer(Vector2 v)
        {
            if (v.X > bounds.X && v.X < (bounds.Width + bounds.X) &&
                v.Y > bounds.Y && v.Y < (bounds.Height + bounds.Y))
                return true;
            return false;
        }
        public PassButton getPassBtn()
        {
            return passButton;
        }

        public void setName(String n)
        {
            playerName = n;
        }
        public String getName()
        {
            return playerName;
        }
        public void setScore(int s)
        {
            score = s;
        }
        public int getScore()
        {
            return score;
        }
        public void setRound(int r)
        {
            round = r;

        }
        public int getRound()
        {
            return round;
        }
    }
}
